package com.uidt.project.ptsj.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.uidt.common.utils.DateUtils;
import com.uidt.common.utils.SecurityUtils;
import com.uidt.framework.security.LoginUser;
import com.uidt.project.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.uidt.project.ptsj.mapper.PtsjXfzcMapper;
import com.uidt.project.ptsj.domain.PtsjXfzc;
import com.uidt.project.ptsj.service.IPtsjXfzcService;

/**
 * 消防自查
Service业务层处理
 * 
 * @author uidt
 * @date 2024-08-27
 */
@Service
public class PtsjXfzcServiceImpl implements IPtsjXfzcService 
{
    @Autowired
    private PtsjXfzcMapper ptsjXfzcMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询消防自查

     * 
     * @param id 消防自查
主键
     * @return 消防自查

     */
    @Override
    public PtsjXfzc selectPtsjXfzcById(Long id)
    {
        return ptsjXfzcMapper.selectPtsjXfzcById(id);
    }

    /**
     * 查询消防自查
列表
     * 
     * @param ptsjXfzc 消防自查

     * @return 消防自查

     */
    @Override
    public List<PtsjXfzc> selectPtsjXfzcList(PtsjXfzc ptsjXfzc)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        // 自定义datascope，根据用户角色的自定义数据权限，拼接查询sql
        if(user!=null){
            String sqlDeptcode = sysDeptService.getDeptAuthByCodeForPlaceAndHouse(user.getUser().getDept().getDeptcode());
            if(sqlDeptcode!=null){
                StringBuilder dataScope = new StringBuilder();
                dataScope.append("(").append("p.gxdwdm like '").append(sqlDeptcode).append("%'").append(")");
                Map<String,Object> paramMap = new HashMap<>();
                paramMap.put("MydataScope","("+dataScope.toString()+")");
                ptsjXfzc.setParams(paramMap);
            }
        }
        return ptsjXfzcMapper.selectPtsjXfzcList(ptsjXfzc);
    }

    /**
     * 新增消防自查

     * 
     * @param ptsjXfzc 消防自查

     * @return 结果
     */
    @Override
    public int insertPtsjXfzc(PtsjXfzc ptsjXfzc)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        Date now = new Date();
        String zcsjStr = DateUtils.dateTimeNow(DateUtils.YYYYMMDD);
        ptsjXfzc.setZcsj(zcsjStr);
        ptsjXfzc.setCreatedTime(now);
        ptsjXfzc.setCreatedBy(user.getUsername());
        return ptsjXfzcMapper.insertPtsjXfzc(ptsjXfzc);
    }

    /**
     * 修改消防自查

     * 
     * @param ptsjXfzc 消防自查

     * @return 结果
     */
    @Override
    public int updatePtsjXfzc(PtsjXfzc ptsjXfzc)
    {
        ptsjXfzc.setUpdateTime(DateUtils.getNowDate());
        return ptsjXfzcMapper.updatePtsjXfzc(ptsjXfzc);
    }

    /**
     * 批量删除消防自查

     * 
     * @param ids 需要删除的消防自查
主键
     * @return 结果
     */
    @Override
    public int deletePtsjXfzcByIds(Long[] ids)
    {
        return ptsjXfzcMapper.deletePtsjXfzcByIds(ids);
    }

    /**
     * 删除消防自查
信息
     * 
     * @param id 消防自查
主键
     * @return 结果
     */
    @Override
    public int deletePtsjXfzcById(Long id)
    {
        return ptsjXfzcMapper.deletePtsjXfzcById(id);
    }
}
